<template>
  <div class="trade-container">
    <h2 class="title">填写并核对订单信息</h2>
    <div class="content">
      <h3 class="receive">收货人信息:</h3>

      <!-- 每个address容器，就是一个收货人信息 -->
      <div class="address clearFix" v-for="address in tradeInfo.userAddressList" :key="address.id">
        <span class="username" :class="{ selected: address.isDefault === '1' }">姓名：{{ address.consignee }}</span>
        <p>
          <span class="s1">联系方式：{{ address.phoneNum }}</span>
          <span class="s2">收货地址：{{ address.userAddress }}</span>
          <span class="s3" v-show="address.isDefault === '1'">默认地址</span>
        </p>
      </div>
      <div class="line"></div>
      <h3 class="pay">支付方式:</h3>
      <div class="address clearFix">
        <span class="username selected" style="cursor: pointer">支付宝</span>
      </div>
      <div class="line"></div>
      <h5 class="pay">商品清单:</h5>
      <!-- 商品清单 -->
      <div class="detail">
        <!-- 一个ul就是一个商品 -->
        <ul class="list clearFix" v-for="detail in tradeInfo.detailArrayList" :key="detail.skuId">
          <!-- 图片 -->
          <li>
            <img :src="detail.imgUrl" alt="">
          </li>
          <!-- 商品名称 -->
          <li>
            <p>{{ detail.skuName }}</p>
          </li>
          <!-- 商品单价 -->
          <li>
            <h3>￥{{ detail.orderPrice }}</h3>
          </li>
          <!-- 商品数量 -->
          <li class="num">
            X{{ detail.skuNum }}
          </li>
          <li class="status">有货</li>
        </ul>
      </div>

      <div class="bbs">
        <h3>买家留言: </h3>
        <textarea v-model="orderComment" placeholder="建议留言前先与商家沟通确认" class="remarks-cont"></textarea>
      </div>
    </div>

    <div class="trade">

      <div class="money clearFix">
        <ul>
          <li>
            <!-- 商品总价 -->
            <span>品类：{{ tradeInfo.totalNum }} &nbsp;</span>
            <span>总计金额：¥{{ tradeInfo.originalTotalAmount }}</span>
          </li>
        </ul>
      </div>

      <!-- 应付金额 -->
      <div class="price">应付金额：<span>¥{{ tradeInfo.totalAmount }}</span></div>
      <!-- 邮寄地址 -->
      <div class="receiveInfo">
        <span>寄送至：{{ showAddress ? showAddress.userAddress : '请选择寄送地址' }}</span>
        <span>收货人信息：{{ showAddress ? showAddress.consignee : '请填写收货人信息' }}</span>
        <span>联系方式：{{ showAddress ? showAddress.phoneNum : '请填写联系方式' }}  </span>
      </div>
    </div>
    <div class="sub clearFix">
      <a class="subBtn" @click="submitOrder">提交订单</a>
    </div>

  </div>
</template>

<script>

import { getTradeInfo,getSubmitOrder } from "@/api/api"
export default {
  name: "TradeIndex",
  data() {
    return {
      tradeInfo: {
        userAddressList: [] // 初始化为空数组
      },
      orderComment: '' //买家留言
    }
  },
  mounted() {
    this.getTradeInfo();
  },
  computed: {
    showAddress() {
      return this.tradeInfo.userAddressList.find(item => item.isDefault === '1') || null;
    }
  },
  methods: {
    // 获取交易订单信息
    async getTradeInfo() {
      const res = await getTradeInfo();
      if (res.code === 200) {
        this.tradeInfo = res.data;
        // console.log(this.tradeInfo);
      } else {
        this.$message.error(`获取交易信息失败:${res.message}`);
      }
    },

    // 提交订单
    async submitOrder() {
      //收集需要提交的参数
      const{ orderComment } = this;
      const { tradeNo,detailArrayList } = this.tradeInfo;
     // 检查 showAddress 是否存在
  const { userAddress = '默认地址', phoneNum = '暂无联系方式', consignee = '未知' } = 
    this.showAddress || {}; // 使用空对象解构避免错误
      //新对象包含所有需要提交的参数
      const obj = {
        orderComment,
        consignee,
        consigneeTel: phoneNum,
        deliveryAddress: userAddress,
        orederDetailList: detailArrayList,
        paymentWay: 'ONLINE'
      }
      //发请求去提交订单（记得要携带【交易编号】）返回的data是订单编号
      const res = await getSubmitOrder(tradeNo,obj);
      if (res.code === 200) {
        this.$message.success(`订单提交成功，订单编号：${res.data}`);
        //跳转到订单详情页面
        this.$router.push({ path: '/pay?id='+ res.data });
      } else {
        this.$message.warning(`订单提交失败:${res.message}`);
        console.log(res);
      }
    }
  }

}
</script>

<style lang="less" scoped>
.trade-container {
  .title {
    width: 1200px;
    margin: 20px auto;
    font-size: 26px;
  }

  .content {
    width: 1200px;
    margin: 10px auto 0;
    border: 1px solid rgb(221, 221, 221);
    padding: 25px;
    box-sizing: border-box;

    .receive,
    .pay {
      line-height: 36px;
      font-size: 22px;
      margin: 18px 0;
    }

    .address {
      margin-bottom: 10px;

      .username {
        display: flex;
        align-items: center;
        justify-content: center;
        width: 130px;
        height: 40px;
        font-size: 18px;
        text-align: center;
        border: 1px solid #ddd;
        position: relative;
      }

      .username::after {
        content: "";
        display: none;
        width: 13px;
        height: 13px;
        position: absolute;
        right: 0;
        bottom: 0;
        background: url(./images/choosed.png) no-repeat;
      }

      .username.selected {
        border-color: #e1251b;
      }

      .username.selected::after {
        display: block;
      }

      p {
        width: 610px;
        float: left;
        line-height: 30px;
        margin-left: 10px;
        padding-left: 5px;
        cursor: pointer;

        .s1 {
          float: left;

        }

        .s2 {
          float: left;
          margin: 0 5px;
        }

        .s3 {
          float: left;
          width: 56px;
          height: 24px;
          line-height: 24px;
          margin-left: 10px;
          background-color: #878787;
          color: #fff;
          margin-top: 3px;
          text-align: center;
        }
      }

      p:hover {
        background-color: #ddd;
      }
    }

    .line {
      height: 1px;
      background-color: #ddd;
    }

    .way {
      width: 1080px;
      height: 110px;
      background: #f4f4f4;
      padding: 15px;
      margin: 0 auto;


      .info {
        margin-top: 20px;

        .s1 {
          float: left;
          border: 1px solid #ddd;
          width: 120px;
          height: 30px;
          line-height: 30px;
          text-align: center;
          margin-right: 10px;
        }

        p {
          line-height: 30px;
        }
      }
    }

    .detail {
      width: 1080px;
      border-radius: 5px;
      padding: 15px;

      img {
        // width: 100px;
        height: 100px;
        box-shadow: 0 0 1px #ccc;
      }

      h5 {
        line-height: 50px;
      }

      .list {
        display: flex;
        justify-content: space-between;
        border: 1px solid #ddd;
        padding: 25px;
        margin-bottom: 5px;
        height: 100px;

        .num,
        .status {
          height: 100px;
          line-height: 100px;
        }

        li {
          display: flex;
          flex-direction: column;
          justify-content: center;

          p {
            max-width: 300px;
            line-height: 20px;
          }

          h4 {
            color: #c81623;
            font-weight: 400;
          }

          h3 {
            height: 100px;
            line-height: 100px;
            color: #e12228;
          }
        }
      }
    }

    .bbs {
      margin-bottom: 15px;

      h3 {
        font-size: 22px;
        margin-bottom: 10px;
      }

      textarea {
        padding: 10px;
        width: 95%;
        border-color: #e4e2e2;
        line-height: 1.8;
        outline: none;
        resize: none;
      }
    }

    .bill {
      h5 {
        line-height: 50px;
      }

      div {
        padding-left: 15px;
      }
    }
  }


  .trade {
    // display: flex;
    box-sizing: border-box;
    width: 1200px;
    padding: 10px;
    margin: 10px auto;
    background-color: #f4f4f4;
    border: 1px solid #ddd;


    .money {
      margin: 10px 20px;

      ul {
        // width: 240px;

        li {
          line-height: 30px;
          list-style: none;

          i,
          span {
            font-size: 22px;
            color: #e12228;
          }
        }
      }
    }

    .price {
      margin: 10px 20px;
      font-size: 22px;
      color: #e12228;
      font-weight: 400;
    }

    .receiveInfo {
      display: flex;
      flex-direction: column;
      margin: 10px 20px;

      span {
        font-size: 22px;
        color: #666;
        margin: 5px 0px;
      }
    }
  }

  .sub {
    width: 1200px;
    margin: 20px auto;
    text-align: right;

    .subBtn {
      display: inline-block;
      width: 150px;
      font-size: 20px;
      padding: 15px;
      text-align: center;
      background-color: #e12228;
      color: #fff;
      border-radius: 5px;
      cursor: pointer;
    }
  }
}
</style>